#!/usr/bin/env python
# encoding: utf-8
'''
@author: zhaowenpeng
@contact: winston@peipeiyun.com
@software: garner
@file: page.py
@time: 2020/7/20 10:39 上午
@desc:
'''

import redis
import pickle

class MyRedis(object):
    """ redis 单例模块 """
    redis = None
    _instance = None

    def __init__(self, host, port, db, password):
        self.pool = redis.ConnectionPool(host=host, port=port, db=db, password=password, decode_responses=True)
        self._conn = redis.Redis(connection_pool=self.pool)

    @classmethod
    def getInstance(cls, host, port, password, decode_responses):
        if cls._instance is None:
            cls._instance = cls(host, port, password, decode_responses)
        return cls._instance

    def setex(self, name, time, value):
        return self.redis.setex(name, time, pickle.dumps(value))

    def delete(self, *names):
        return self.redis.delete(*names)

    def get(self, name):
        data = self.redis.get(name)
        try:
            assert data is not None
            return pickle.loads(data)
        except AssertionError as ae:
            return None
